$m(function (args) {
    var $this = this.element;
    var serviceid = args.id;
    //灰度服务类型
    var grayType = window.audit.grayType;
    //判断是否为流服务
    var isStream = window.audit.serviceType == "stream";
    var init = function () {
        initQuery();
        $this.find("#gridDiv").height($this.parent().height() - $this[0].offsetHeight);
        initServiceLogGrid();
    }
    var operation = {};
    operation['OP_PUBLISH'] = TempoUtils.locale("aiview.auditPublish");
    operation['OP_SAVE_CONFIG'] = TempoUtils.locale("aiview.auditSaveConfig");
    operation['OP_START'] = TempoUtils.locale("aiview.auditStart");
    operation['OP_STOP'] = TempoUtils.locale("aiview.auditStop");
    operation['OP_START_GRAY'] = TempoUtils.locale("aiview.OP_START_GRAY");
    operation['OP_STOP_FORBIDDEN'] = TempoUtils.locale("aiview.OP_STOP_FORBIDDEN");
    operation['OP_ASYNC_ENABLE'] = TempoUtils.locale("aiview.auditEnable");
    operation['OP_ASYNC_DISABLE'] = TempoUtils.locale("aiview.auditDisable");
    operation['OP_PULLSHELVES'] = TempoUtils.locale("mine.pullshelves");
    operation['OP_PUTSHELVES'] = TempoUtils.locale("mine.putshelves");

    var initServiceLogGrid = function () {
        $this.find('#mine-opt-servicelog-grid').datagrid({
            datatype: "json",
            url: contextPath + '/api/ai/app/audit/list?id=' + serviceid,
            fit: true,
            rownumbers: true,
            mtype: 'GET',
            autowidth: true,
            shrinkToFit: true,
            viewrecords: true,
            striped: true,
            colNames: ['AUDIT_ID',
                //操作时间
                TempoUtils.locale("aiview.opttime"),
                //操作人
                TempoUtils.locale("aiview.optuser"),
                //操作
                TempoUtils.locale("aiview.opt"),
                //操作内容
                TempoUtils.locale("aiview.remark")
            ],
            colModel: [{
                name: 'AUDIT_ID',
                index: 'AUDIT_ID',
                hidden: true,
                sortable: false
            }, {
                name: 'CREATE_TIME',
                index: 'CREATE_TIME',
                width: 60,
                sortable: false,
                formatter: function (obj) {
                    return DateUtils.formatDateTime(obj);
                }
            }, {
                name: 'NAME',
                index: 'NAME',
                sortable: false,
                width: 60,
                sortable: false
            }, {
                name: 'OPERATION',
                index: 'OPERATION',
                sortable: false,
                width: 60,
                sortable: false,
                formatter: function (obj) {
                    return TempoUtils.locale("aiview." + obj)
                },
            }, {
                name: 'CONTENT',
                index: 'CONTENT',
                formatter: function (obj, options, rowObject) {
                    //获取操作类型
                    var operationType = rowObject.OPERATION;
                    //流服务启动增加下载启动日志按钮
                    if (isStream && operationType == "OP_START") {
                        //审计日志ID
                        var auditId = rowObject.AUDIT_ID;
                        var downLoad = '<span style="margin-left: 10px" auditId="' + auditId + '"' +
                            'class="tempo-datagrid-actionbtn ui-icon tempo-ui-icon tempo-ui-icon-download" title="下载启动日志"></span>';
                        var content = operation[obj] || TempoMineUtils.encodeHTML(obj);
                        if (content.indexOf("失败") > -1) {
                            return "调用服务启动失败" + downLoad;
                        } else {
                            return "服务启动" + downLoad;
                        }
                    } else {
                        return operation[obj] || TempoMineUtils.encodeHTML(obj);
                    }

                },
                width: 160,
                sortable: false
            }
            ],
            pagination: true,
            loadComplete: function (result) {
                var width = $this.find('#gridDiv').width();
                $this.find('#gbox_mine-opt-servicelog-grid').css("width", width + 4 + "px");
                //给下载启动日志按钮增加点击事件
                $this.find('#mine-opt-servicelog-grid').find("td[aria-describedby$='_CONTENT']")
                    .find(".tempo-ui-icon-download").unbind("click").bind("click", function (e) {
                    //审计日志ID
                    var auditId = $(this).attr("auditId");
                    //下载日志等监管提供下载
                    TempoUtils.download({
                        url: contextPath + "/api/ai/mine/stream/down-start-log",
                        data: {
                            auditId: auditId,
                        }
                    });
                })
            }
        })
    }

    var initQuery = function () {
        var form = $this.find('.queryForm');
        TempoUtils.parserUi(form, {});
        form.find('[name="starttime"]').datepicker({
            dateFormat: "yy-mm-dd",
            yearRange: "c-100:",
            //设置日期的最大时间和最小时间
            minDate: DateUtils.formatDate(new Date(new Date().getFullYear() - 100, 0, 1)),
            maxDate: DateUtils.formatDate(new Date(new Date().getFullYear() + 1, 0, 1)),
            changeYear: true,
            changeMonth: true,
            onClose: function (selectedDate) {
                var d = $(this).datepicker('getDate');
                if (d == null) {
                    return;
                }
                var d2 = new Date(d.getTime());
                var d2str = d2.Format('yyyy-MM-dd');
                form.find('[name="endtime"]').datepicker("option", "minDate", d2str);
            },
            onSelect: function (value) {
            },
            onChangeMonthYear: function (year, month, inst) {
                var month = inst.selectedMonth + 1;
                var day = inst.selectedDay;
                var format = function (v) {
                    return String(v).length > 1 ? v : ('0' + v)
                }
                $(this).val(inst.selectedYear + '-' + format(month) + '-' + format(inst.selectedDay));
                $.datepicker._hideDatepicker(this);
            }
        });
        form.find('[name="endtime"]').datepicker({
            dateFormat: "yy-mm-dd",
            yearRange: "c-100:",
            //设置日期的最大时间和最小时间
            minDate: DateUtils.formatDate(new Date(new Date().getFullYear() - 100, 0, 1)),
            maxDate: DateUtils.formatDate(new Date(new Date().getFullYear() + 1, 0, 1)),
            changeYear: true,
            changeMonth: true,
            onSelect: function (value) {
            },
            onClose: function (selectedDate) {
                var d = $(this).datepicker('getDate');
                if (d == null) {
                    return;
                }
                var d2 = new Date(d.getTime());
                var d2str = d2.Format('yyyy-MM-dd');
                form.find('[name="starttime"]').datepicker("option", "maxDate", d2str);
            },
            onChangeMonthYear: function (year, month, inst) {
                var month = inst.selectedMonth + 1;
                var day = inst.selectedDay;
                var format = function (v) {
                    return String(v).length > 1 ? v : ('0' + v)
                }
                $(this).val(inst.selectedYear + '-' + format(month) + '-' + format(inst.selectedDay));
                $.datepicker._hideDatepicker(this);
            }
        });
        form.find('.queryBtn').button({
            click: function () {
                var starttime = form.find('[name="starttime"]').datepicker('getDate');
                var endtime = form.find('[name="endtime"]').datepicker('getDate');
                var opt = form.find('[name="opt"]').val();
                var optuser = form.find('[name="optuser"]').val();
                var data = {
                    starttime: starttime ? DateUtils.formatDate(starttime, 'yyyy-MM-dd') : null,
                    endtime: endtime ? DateUtils.formatDate(endtime, 'yyyy-MM-dd') : null,
                    opt: opt,
                    optuser: optuser,
                }
                $this.find('#mine-opt-servicelog-grid').datagrid('reloadGrid', data);
                return false;
            }
        });
        form.find('.resetBtn').button({
            click: function () {
                form[0].reset();
                form.find('[name="endtime"]').datepicker("option", "minDate", DateUtils.formatDate(new Date(new Date().getFullYear() - 100, 0, 1)));
                form.find('[name="starttime"]').datepicker("option", "maxDate", DateUtils.formatDate(new Date(new Date().getFullYear() + 1, 0, 1)));
                form.find('[name="opt"]').val('').selectmenu('refresh');
                form.find('[name="optuser"]').val('');
                return false;
            }
        });
        //灰度服务服务审计页面操作内容下拉框内容修改
        form.find('select[name="opt"]').selectmenu({
            open: function () {
                form.find('[name="opt"]').selectmenu("hideItems", ["OP_START_GRAY", "OP_STOP_FORBIDDEN"]);
                if ("gray" == grayType) {
                    form.find('[name="opt"]').selectmenu("hideItems", ["OP_FLOW_UPDATE", "OP_START", "OP_STOP", "OP_PUBLISH"]);
                    form.find('[name="opt"]').selectmenu("showItems", ["OP_START_GRAY", "OP_STOP_FORBIDDEN"]);
                }
            }
        });
    }

    init();
    var timeout = 0;
    window.addEventListener('resize', function () {
        if (timeout != 0) {
            clearTimeout(timeout);
        }
        timeout = setTimeout(function () {
            $this.find('#mine-opt-servicelog-grid').datagrid("reloadGrid");
            timeout = 0;
        }, 200)
    });
});	